package com.neusoft.controller;

import com.neusoft.base.BaseController;
import com.neusoft.model.LoginObj;
import com.neusoft.service.ManageService;
import com.neusoft.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/login")
@Api(tags = "登录接口")
public class LoginController extends BaseController {

    @Resource
    private ManageService manageService;
    @Resource
    private UserService userService;
    @Resource
    private HttpServletRequest request;

    @PostMapping
    @ApiOperation("登录")
    public Object login(@RequestBody LoginObj loginObj) {
        Object login = null;
        if (loginObj.isManage()) {
            login = manageService.login(loginObj.getUsername(), loginObj.getPassword());
        } else {
            login = userService.login(loginObj.getUsername(), loginObj.getPassword());
        }
        if (null != login) {
            request.getSession().setAttribute("user", login);
            request.getSession().setMaxInactiveInterval(30);
            return login;
        }
        throw new RuntimeException("登录失败：用户名或者密码错误");
    }


}
